package com.heitian.ssm.service.impl;

import com.heitian.ssm.dao.AlbumDao;
import com.heitian.ssm.dao.AttachDao;
import com.heitian.ssm.model.Album;
import com.heitian.ssm.model.Attach;
import com.heitian.ssm.model.PageBean;
import com.heitian.ssm.service.AlbumService;
import com.heitian.ssm.service.AttachService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * 作者： 陈建峰
 * 创建时间： 18/3/27 下午10:26
 * 版本： [1.0, 18/3/27]
 * 描述： 相册处理
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class AlbumServiceImpl implements AlbumService{

    @Resource
    private AlbumDao albumDao;

    /**
     * 保存相册
     * @param album
     */
    public void saveAlbum(Album album) {
        albumDao.saveAlbum(album.AlbumGuid,album.AlbumName,album.OwnerUserGuid,album.CreateTime,album.UpdateTime,
                album.SeeTimes,album.IsPublic,album.Content,album.Mark,album.ZanNum,album.AllCount,album.CoverUrl);
    }

    /**
     * 删除相册
     * @param albumGuid
     */
    public void deleteAlbum(String albumGuid) {
      albumDao.deleteAlbum(albumGuid);
    }

    /**
     * 获取某用户的相册数量
     * @param OwnerUserGuid
     * @return
     */
    public int getAllAlbumCountByOwnerUserGuid(String OwnerUserGuid) {
        return albumDao.getAllAlbumCountByOwnerUserGuid(OwnerUserGuid);
    }

    /**
     * 获取某用户的相册列表
     * @param OwnerUserGuid
     * @param index
     * @param currentPageCount
     * @return
     */
    public PageBean<Album> getAllAlbumByOwnerUserGuid(String OwnerUserGuid, int index, int currentPageCount) {
        //封装一个PageBean 返回web层
        PageBean<Album> pageBean = new PageBean<Album>();
        //1、封装当前页
        pageBean.setCurrentPage(index);
        //2、封装每页显示的条数
        pageBean.setCurrentCount(currentPageCount);
        //3、封装总条数
        int totalCount = albumDao.getAllAlbumCountByOwnerUserGuid(OwnerUserGuid);
        pageBean.setTotalCount(totalCount);
        //4、封装总页数
        int totalPage = (int) Math.ceil(1.0*totalCount/currentPageCount);
        pageBean.setTotalPage(totalPage);
        //5、当前页显示的数据
        // select * from product where cid=? limit ?,?
        // 当前页与起始索引index的关系
        int indexes = (index-1)*currentPageCount;
        List<Album> list = albumDao.getAllAlbumByOwnerUserGuid(OwnerUserGuid,indexes,currentPageCount);
        pageBean.setList(list);
        return pageBean;
    }

    /**
     * 获取所有相册数量
     * @return
     */
    public int getAllAlbumCount() {
        return albumDao.getAllAlbumCount();
    }

    /**
     * 获取相册信息
     * @param AlbumGuid
     * @return
     */
    public Album getAlbumByAlbumGuid(String AlbumGuid) {
        return albumDao.getAlbumByAlbumGuid(AlbumGuid);
    }
}
